package com.digua.tank;

import java.awt.*;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.util.ArrayList;
import java.util.List;

/**
 * @author wangdigua
 * @title
 * @date 2020/11/7 10:23 上午
 * @since 1.0.0
 */
public class TankFrame extends Frame {

    public static final int GAME_WIDTH = 1000;
    public static final int GAME_HEIGHT = 800;
    private Tank myTank = new Tank(200, 400, Dir.RIGHT, this,Group.GOOD);
    //子弹容器列表
    public List<Bullet> bullets = new ArrayList<Bullet>();
    //敌人坦克
    public List<Tank> tanks = new ArrayList<Tank>();
    //爆炸效果
    public List<Explod> explods = new ArrayList<Explod>();


    public TankFrame() throws HeadlessException {
        this.setSize(GAME_WIDTH, GAME_HEIGHT);
        this.setResizable(false);
        this.setVisible(true);
        this.setTitle("Tank War");
        this.addWindowListener(new WindowAdapter() {
            @Override
            public void windowClosing(WindowEvent e) {
                System.exit(0);
            }
        });
        this.addKeyListener(new MyKeyListener());
        myTank.setSPEED(5);
        tanks.add(myTank);
//        new Audio("audio/war1.wav").player();
    }

    /**
     * 系统自动调用，传递一个画笔
     * @param g
     */
    @Override
    public void paint(Graphics g) {
        //打印出子弹数量：
        Color color = g.getColor();
        g.setColor(Color.white);
        g.drawString("子弹的数量:" + bullets.size(),10,50);
        g.drawString("坦克的数量:" + tanks.size(),10,70);
        //此处非常重要，就是打印出数据后需要把之前的颜色设置回去
        g.setColor(color);

        //这里不要直接把tank.x 这样取属性，这样就破坏了封装的特性
        myTank.paint(g);
        //画出子弹,此处循环注意可能会有坑，for(T t : List) 这样遍历不能在其他地方删除，只能在这个循环内删除，Java的坑
        for (int i = 0; i < bullets.size(); i++) {
            bullets.get(i).paint(g);
        }
        //画出敌人坦克
        for (int i = 0; i < tanks.size(); i++) {
            tanks.get(i).paint(g);
        }
        //碰撞检查
        for (int i = 0; i < bullets.size(); i++) {
            for (int j = 0; j < tanks.size(); j++) {
                bullets.get(i).collideWith(tanks.get(j));
            }
        }
        //画出爆炸效果
        for (int i = 0; i < explods.size(); i++) {
            explods.get(i).paint(g);
        }

    }

    //双缓存解决屏幕闪烁问题，现在内存中生成一张图片并在图片上画好tank 和子弹 然后再一次性画到屏幕上
    Image offscreenImage = null;
    @Override
    public void update(Graphics g) {
        if (offscreenImage == null) {
            offscreenImage = this.createImage(GAME_WIDTH, GAME_HEIGHT);
        }
        Graphics goffScreen = offscreenImage.getGraphics();
        Color color = goffScreen.getColor();
        goffScreen.setColor(Color.BLACK);
        goffScreen.fillRect(0, 0, GAME_WIDTH, GAME_HEIGHT);
        goffScreen.setColor(color);
        paint(goffScreen);
        g.drawImage(offscreenImage, 0, 0, null);
    }

    /**
     * 键盘操作监听
     */
    class MyKeyListener extends KeyAdapter {
        //方向状态
        boolean left = false;
        boolean right = false;
        boolean up = false;
        boolean down = false;

        @Override
        public void keyPressed(KeyEvent e) {
            int key = e.getKeyCode();
            switch (key) {
                case KeyEvent.VK_A:
                case KeyEvent.VK_LEFT:
                    left = true;
                    break;
                case KeyEvent.VK_D:
                case KeyEvent.VK_RIGHT:
                    right = true;
                    break;
                case KeyEvent.VK_S:
                case KeyEvent.VK_DOWN:
                    down = true;
                    break;
                case KeyEvent.VK_W:
                case KeyEvent.VK_UP:
                    up = true;
                    break;
                default:
                    break;
            }
            //按下键盘后就设置方向，
            setMainTankDir();
            //重画，底层调用paint
            repaint();
        }

        @Override
        public void keyReleased(KeyEvent e) {
            switch (e.getKeyCode()) {
                case KeyEvent.VK_A:
                case KeyEvent.VK_LEFT:
                    left = false;
                    break;
                case KeyEvent.VK_D:
                case KeyEvent.VK_RIGHT:
                    right = false;
                    break;
                case KeyEvent.VK_S:
                case KeyEvent.VK_DOWN:
                    down = false;
                    break;
                case KeyEvent.VK_W:
                case KeyEvent.VK_UP:
                    up = false;
                    break;
                case KeyEvent.VK_SPACE:
                    //弹起空格打出一颗子弹
                    myTank.fire();
                    break;
                default:
                    break;
            }
            //按键释放后也需要设置主战坦克的方向和状态
            setMainTankDir();
        }

        private void setMainTankDir() {
            if (!left && !right && !up && !down) {
                myTank.setMoving(false);
            } else {
                myTank.setMoving(true);
                if (left) myTank.setDir(Dir.LEFT);
                if (right) myTank.setDir(Dir.RIGHT);
                if (up) myTank.setDir(Dir.UP);
                if (down) myTank.setDir(Dir.DOWN);
            }
        }
    }

    public Tank getMyTank() {
        return myTank;
    }

    public void setMyTank(Tank myTank) {
        this.myTank = myTank;
    }

}
